/*
 * Copyright 2014 LinkedIn Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

var extendedViewPanels = {};
var extendedDataModels = {};
var openJobDisplayCallback = function (nodeId, flowId, evt) {
  console.log("Open up data");

  /*
  $("#flowInfoBase").before(cloneStuff);
  var requestURL = contextURL + "/manager";

  $.get(
      requestURL,
      {"project": projectName, "ajax":"fetchflownodedata", "flow":flowId, "node": nodeId},
      function(data) {
      var graphModel = new azkaban.GraphModel();
      graphModel.set({id: data.id, flow: data.flowData, type: data.type, props: data.props});

      var flowData = data.flowData;
      if (flowData) {
        createModelFromAjaxCall(flowData, graphModel);
      }

      var backboneView = new azkaban.FlowExtendedViewPanel({el:cloneStuff, model: graphModel});
      extendedViewPanels[nodeInfoPanelID] = backboneView;
      extendedDataModels[nodeInfoPanelID] = graphModel;
      backboneView.showExtendedView(evt);
      },
      "json"
    );
    */
};

var createNewPanel = function (node, model, evt) {
  var parentPath = node.parentPath;

  var nodeInfoPanelID = parentPath ? parentPath + ":" + node.id + "-info" : node.id + "-info";
  var cloneStuff = $("#flowInfoBase").clone();
  cloneStuff.data = node;
  $(cloneStuff).attr("id", nodeInfoPanelID);
  $("#flowInfoBase").before(cloneStuff);

  var backboneView = new azkaban.FlowExtendedViewPanel(
      {el: cloneStuff, model: model});
  node.panel = backboneView;
  backboneView.showExtendedView(evt);
};

var closeAllSubDisplays = function () {
  $(".flowExtendedView").hide();
};

var nodeClickCallback = function (event, model, node) {
  var type = node.type;
  var flowId = node.parent.flow;
  var jobId = node.id;

  var requestURL = contextURL + "/manager?project=" + projectName + "&flow=" + flowId + "&job=" + jobId;
  var logURL = contextURL + "/executor?execid=" + execId + "&job=" + node.nestedId + "&attempt=" + node.attempt;
  var menu = [];

  if (type == "flow") {
    var flowRequestURL = contextURL + "/manager?project=" + projectName
        + "&flow=" + node.flowId;
    if (node.expanded) {
      menu = [
        {
          title: "Collapse Flow...", callback: function () {
            model.trigger("collapseFlow", node);
          }
        },
        {
          title: "Collapse All Flows...", callback: function () {
            model.trigger("collapseAllFlows", node);
          }
        }
      ];
    }
    else {
      menu = [
        {
          title: "Expand Flow...", callback: function () {
            model.trigger("expandFlow", node);
          }
        },
        {
          title: "Expand All Flows...", callback: function () {
            model.trigger("expandAllFlows", node);
          }
        }
      ];
    }

    $.merge(menu, [
      //  {title: "View Properties...", callback: function() {openJobDisplayCallback(jobId, flowId, event)}},
      {break: 1},
      {
        title: "Open Flow...", callback: function () {
          window.location.href = flowRequestURL;
        }
      },
      {
        title: "Open Flow in New Window...", callback: function () {
          window.open(flowRequestURL);
        }
      },
      {break: 1},
      {
        title: "Open Properties...", callback: function () {
          window.location.href = requestURL;
        }
      },
      {
        title: "Open Properties in New Window...", callback: function () {
          window.open(requestURL);
        }
      },
      {break: 1},
      {
        title: "Center Flow", callback: function () {
          model.trigger("centerNode", node);
        }
      }
    ]);
  }
  else {
    // this applies for type != 'flow' ie. job nodes in 2 cases:
    // 1. Flow -> Graph tab
    // 2. Execution -> Graph tab
    menu = [];
    $.merge(menu, [
      //  {title: "View Properties...", callback: function() {openJobDisplayCallback(jobId, flowId, event)}},
      //  {break: 1},
      {
        title: "Open Job...", callback: function () {
          window.location.href = requestURL;
        }
      },
      {
        title: "Open Job in New Window...", callback: function () {
          window.open(requestURL);
        }
      },
      {break: 1},
      {
        title: "Center Job", callback: function () {
          model.trigger("centerNode", node);
        }
      }
    ]);
    // Include job log links in job context menu unless job hasn't been started
    if (node.status !== 'READY' && node.status !== 'SKIPPED'
        && node.status !== 'DISABLED' && node.status !== 'CANCELLED'
        && node.status !== 'DISPATCHING') {
      // For "Flow Graph" (not an execution) node.status = READY, so this
      // condition also works correctly for it
      $.merge(menu, [
        {break: 1},
        {
          title: "Open Log...", callback: function () {
            window.location.href = logURL;
          }
        },
        {
          title: "Open Log in New Window...", callback: function () {
            window.open(logURL);
          }
        }
      ]);
    }
  }
  contextMenuView.show(event, menu);
};

var jobClickCallback = function (event, model, node) {
  var type = node.type;
  var flowId = node.parent.flow;
  
  var requestURL = contextURL + "/manager?project=" + projectName + "&flow=" + flowId + "&job=" + node.id;
  var menu;

  if (type == "flow") {
    var flowRequestURL = contextURL + "/manager?project=" + projectName + "&flow=" + node.flowId;

    menu = [
      //  {title: "View Properties...", callback: function() {openJobDisplayCallback(jobId, flowId, event)}},
      //  {break: 1},
      {
        title: "Open Flow...", callback: function () {
          window.location.href = flowRequestURL;
        }
      },
      {
        title: "Open Flow in New Window...", callback: function () {
          window.open(flowRequestURL);
        }
      },
      {break: 1},
      {
        title: "Open Properties...", callback: function () {
          window.location.href = requestURL;
        }
      },
      {
        title: "Open Properties in New Window...", callback: function () {
          window.open(requestURL);
        }
      },
      {break: 1},
      {
        title: "Center Flow", callback: function () {
          model.trigger("centerNode", node);
        }
      }
    ];
  }
  else {
    menu = [
      //  {title: "View Job...", callback: function() {openJobDisplayCallback(jobId, flowId, event)}},
      //  {break: 1},
      {
        title: "Open Job...", callback: function () {
          window.location.href = requestURL;
        }
      },
      {
        title: "Open Job in New Window...", callback: function () {
          window.open(requestURL);
        }
      },
      {break: 1},
      {
        title: "Center Job", callback: function () {
          graphModel.trigger("centerNode", node);
        }
      }
    ];
  }
  contextMenuView.show(event, menu);
};

var edgeClickCallback = function (event, model) {}

var graphClickCallback = function (event, model) {
  var data = model.get("data");
  var flowId = data.flow;
  var requestURL = contextURL + "/manager?project=" + projectName + "&flow=" + flowId;

  var menu = [
    {
      title: "Expand All Flows...", callback: function () {
        model.trigger("expandAllFlows");
        model.trigger("resetPanZoom");
      }
    },
    {
      title: "Collapse All Flows...", callback: function () {
        model.trigger("collapseAllFlows");
        model.trigger("resetPanZoom");
      }
    },
    {break: 1},
    {
      title: "Expand Flows 1 Level...", callback: function () {
        model.trigger("expandFlows1");
        model.trigger("resetPanZoom");
      }
    },
    {
      title: "Expand Flows 2 Levels...", callback: function () {
        model.trigger("expandFlows2");
        model.trigger("resetPanZoom");
      }
    },
    {
      title: "Expand Flows 3 Levels...", callback: function () {
        model.trigger("expandFlows3");
        model.trigger("resetPanZoom");
      }
    },
    {break: 1},
    {
      title: "Open Flow...", callback: function () {
        window.location.href = requestURL;
      }
    },
    {
      title: "Open Flow in New Window...", callback: function () {
        window.open(requestURL);
      }
    },
    {break: 1},
    {
      title: "Center Graph", callback: function () {
        model.trigger("resetPanZoom");
      }
    }
  ];
  contextMenuView.show(event, menu);
};
